﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Reflection.Core
{
    class DummyMemberInfo : IDummyMemberInfo
    {
        IDummyType declaringType;
        string name;
        List<object> attributes = new List<object>();

        public IDummyType DeclaringType
        {
            get { return declaringType; }
            set { declaringType = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 后期初始化: property / attribute
        /// </summary>
        /// <param name="nameSpace"></param>
        /// <param name="fullname"></param>
        /// <param name="name"></param>
        /// <param name="hashcode"></param>
        /// <returns></returns>
        [AddAttribute]
        public void AddAttribute(object type)
        {
            if (type == null)
                throw Exceptions.VerificationFailedException(type);

            attributes.Add(type);
        }

        public virtual object GetCustomAttribute(Type attributeType)
        {
            if (attributeType == null)
                return null;

            foreach (object myAttribute in attributes)
            {
                Type myAttributeType = myAttribute.GetType();

                //2010-08-29 提高性能

                //if (!string.Equals(myAttributeType.FullName, attributeType.FullName))
                //    continue;

                if (!string.Equals(myAttributeType.Name, attributeType.Name))
                    continue;

                if (!string.Equals(myAttributeType.Namespace, attributeType.Namespace))
                    continue;

                return myAttribute;
            }

            return null;
        }

        public virtual object[] GetCustomAttributes(Type attributeType)
        {
            List<object> rlist = new List<object>();

            if (attributeType == null)
                return rlist.ToArray();

            foreach (object myAttribute in attributes)
            {
                Type myAttributeType = myAttribute.GetType();

                //if (!string.Equals(myAttributeType.FullName, attributeType.FullName))
                //    continue;

                if (!string.Equals(myAttributeType.Name, attributeType.Name))
                    continue;

                if (!string.Equals(myAttributeType.Namespace, attributeType.Namespace))
                    continue;

                rlist.Add(myAttributeType);
            }

            return rlist.ToArray();
        }

        public virtual object[] GetCustomAttributes()
        {
            return attributes.ToArray();
        }

        public virtual void Dispose()
        {
            this.declaringType = null;
            this.name = null;
            attributes.Clear();
        }

        public virtual bool Verify()
        {
            if (string.IsNullOrEmpty(Name))
                throw new Exception("missing property name.");

            return true;
        }

        internal class AddAttributeAttribute : Attribute { }
        internal class MetadataTokenAttribute : Attribute { }
    }
}
